<template>
  <div class="login-container">
    <el-form
      class="login-form"
      :rules="loginRules"
      :model="loginForm"
      ref="loginFormRef"
    >
      <h2 class="title">陈本聊天室</h2>
      <el-form-item prop="username">
        <el-input
          placeholder="请输入用户名"
          prefix-icon="User"
          v-model="loginForm.username"
          class="num"
        >
        </el-input>
      </el-form-item>

      <el-form-item prop="password">
        <el-input
          placeholder="请输入密码"
          type="password"
          prefix-icon="Lock"
          v-model="loginForm.password"
        >
        </el-input>
      </el-form-item>

      <el-button class="login-btn" type="primary" @="" @click="doLogin"
        >登录</el-button
      >

      <div class="register-tip">
        <span class="register-tip">还没有账号？</span>
        <el-button type="text" @click="register = true">注册</el-button>
      </div>
    </el-form>

    <!-- 注册对话框 -->
    <el-dialog width="500" title="注册" v-model="register">
      <el-form
        label-width="100px"
        :rules="shloginRules"
        :model="shloginForm"
        ref="reloginFormRef"
      >
        <el-form-item label="账号" class="register-form" prop="username">
          <el-input placeholder="请输入账号" v-model="shloginForm.username" />
        </el-form-item>
        <el-form-item label="密码" class="register-form" prop="password">
          <el-input
            placeholder="请输入账号"
            type="password"
            v-model="shloginForm.password"
          />
        </el-form-item>
        <el-form-item label="确认密码" prop="erpassword">
          <el-input
            placeholder="请输入账号"
            type="password"
            v-model="shloginForm.erpassword"
          />
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button @click="register = false">取消</el-button>
          <el-button type="primary" @click="redoLogin">注册</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup lang="ts">
import { ElMessage } from "element-plus";
import { reactive, ref, shallowReactive } from "vue";
import axios from "../api/axios";
import { useRouter } from "vue-router";

const router = useRouter();

//登录表单数据
const loginForm = reactive({
  username: "",
  password: "",
});

//登录表单校验
const loginRules = reactive({
  username: [
    { required: true, message: "请输入用户名", trigger: "blur" },
    { min: 3, max: 10, message: "长度在3-10个字符之间", trigger: "blur" },
  ],
  password: [
    { required: true, message: "请输入密码", trigger: "blur" },
    { min: 5, max: 15, message: "长度在5-15个字符之间", trigger: "blur" },
  ],
});

//取消注册对话框
const register = ref(false);

//注册表单数据
const shloginForm = reactive({
  username: "",
  password: "",
  erpassword: "",
});
//注册表单校验
const shloginRules = reactive({
  username: [
    { required: true, message: "请输入用户名", trigger: "blur" },
    { min: 3, max: 10, message: "长度在3-10个字符之间", trigger: "blur" },
  ],
  password: [
    { required: true, message: "请输入密码", trigger: "blur" },
    { min: 5, max: 15, message: "长度在5-15个字符之间", trigger: "blur" },
  ],
  erpassword: [
    { required: true, message: "请输入密码", trigger: "blur" },
    //确认两个密码一致
    {
      validator: (rule: any, value: any, callback: any) => {
        if (value === shloginForm.password) {
          callback();
        } else {
          callback("两次密码不一致");
        }
      },
    },
  ],
});

//登录表单实例
const loginFormRef = ref(null);
//注册表单实例
const reloginFormRef = ref(null);



//登录验证
const doLogin = () => {
  loginFormRef.value.validate(async (valid: any) => {
    if (valid) {
      // alert(valid+'登录成功')

      axios.post("/login", loginForm).then((res) => {
        if (res.data.success) {
          ElMessage.success("登录成功")

          localStorage.setItem("user",JSON.stringify(res.data.user));

          //跳转页面
          router.push("/Main");
        } else {
          ElMessage.error(res.data.massage);
        }
      });

      // ElMessage.success('登录成功')
    } else {
      // alert(valid+'登录失败')
      ElMessage.error("登录失败");
    }
  });
};
//注册验证
const redoLogin = () => {
  reloginFormRef.value.validate((valid: any) => {
    if (valid) {
      // alert(valid+'登录成功')
      //发送http请求
      axios.post("/register", shloginForm).then((res) => {
        if (res.data.success) {
          ElMessage.success("注册成功");
          showRegisterDialog.value = false;
        } else {
          ElMessage.error(res.data.massage);
        }
      });

      // http://localhost:5000/register
      // http://localhost:5000/login
    } else {
      // alert(valid+'登录失败')
      ElMessage.error("注册失败");
    }
  });
};

const usr = () => {
  router.push("/Main");
};
</script>

<style scoped lang="scss">
@import "../assets/login.scss";
</style>
